/**
 * @(#) CourseMarksApprovalAction.as
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
import common.*;

import courseMarksApproval.EditCourseMarksApproval;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.validators.Validator;


[Bindable]public var urlPrefix1:String;
[Bindable]public var urlPrefix2:String;
[Bindable]public var infoObject:Object = {};
[Bindable]var details: XML;
[Bindable]var branchDetails: XML;
[Bindable]var specialiationDetails: XML;
[Bindable]var semesterDetails: XML;
[Bindable]var courseList: XML;
[Bindable]var employeeList: XML;
[Bindable]var marksApprovalDetails: XML;

[Embed(source="images/error.png")] private var errorIcon:Class;
[Embed(source="images/success.png")] private var successIcon:Class;
[Embed(source="images/reset.png")] private var resetIcon:Class;
[Embed(source="images/question.png")] private var questionIcon:Class;


/** This function Set Focus on 1st Field Program combobox **/
public function setFirstFocous():void
{
	urlPrefix1=commonFunction.getConstants('url')+"/programCourseType/";
	urlPrefix2=commonFunction.getConstants('url')+"/marksApproval/";
	
	infoObject["time"]=new Date();
	getProgramList.send(infoObject);
	getEmployeeList.send(infoObject);
	Mask.show(commonFunction.getMessages('pleaseWait'));
	programCombo.setFocus();
}

/** get Program List Success Handler **/
public function getProgramSuccess(event:ResultEvent):void{
	
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    details=event.result as XML;
	var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in details.role){	
		detailslist.addItem({id:o.id,description:o.description});	
	}
	programCombo.dataProvider = detailslist;
	programCombo.labelField="description";	
}

/** Failure Handlure **/
public function onFailure(event:FaultEvent):void
{
	Mask.close();
	Alert.show(commonFunction.getMessages('requestFailed'),commonFunction.getMessages('error'),0,null,null,errorIcon);
}

/** Program change Handler **/
public function programChange():void
{
	 infoObject["programId"] = programCombo.selectedItem.id;
	 infoObject["time"]=new Date();
	
	 getBranchList.send(infoObject);
	Mask.show(commonFunction.getMessages('pleaseWait'));
	 
	 branchCombo.enabled=true;
     branchCombo.selectedIndex=-1;
	 refresh();
}

/** get Branch List Success Handler **/
public function getBranchSuccess(event:ResultEvent):void{
	
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    branchDetails=event.result as XML;
	
    var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in branchDetails.role){
		
		detailslist.addItem({id:o.id,description:o.description});
		
	}
	branchCombo.dataProvider = detailslist;
	branchCombo.labelField="description";
}

/** Branch change Handler **/
protected function branchChange():void
{
	specializationCombo.enabled=true;
	specializationCombo.selectedIndex=-1;
	
	infoObject["programId"] = programCombo.selectedItem.id;
	infoObject["branchId"] = branchCombo.selectedItem.id;
	infoObject["time"]=new Date();
	
	getspecializationList.send(infoObject);
	Mask.show(commonFunction.getMessages('pleaseWait'));
	refresh();
}

/** get Specialization List Success Handler **/
public function getspecializationSuccess(event:ResultEvent):void
{
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    specialiationDetails=event.result as XML;
	
	var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in specialiationDetails.role){
		
		detailslist.addItem({id:o.id,description:o.description});
		
	}
	specializationCombo.dataProvider = detailslist;
	specializationCombo.labelField="description";
}
			
/** Specialization change Handler **/
protected function specializationChange():void
{
	semesterCombo.enabled=true;
	semesterCombo.selectedIndex=-1;
				
	infoObject["programId"] = programCombo.selectedItem.id;
	infoObject["branchId"] = branchCombo.selectedItem.id;
	infoObject["specializationId"] = specializationCombo.selectedItem.id;
	infoObject["time"]=new Date();
				
	getSemesterList.send(infoObject);
	Mask.show(commonFunction.getMessages('pleaseWait'));
	refresh();
}	
			
/** get Semester List Success Handler **/
public function getSemesterSuccess(event:ResultEvent):void{
	
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    semesterDetails=event.result as XML;	
	
	var detailslist:ArrayCollection =new ArrayCollection();
	
	for each (var o:Object in semesterDetails.role)
	{
		detailslist.addItem({id:o.id,description:o.description});	
	}
	semesterCombo.dataProvider = detailslist;
	semesterCombo.labelField="description";
}

/** Semester change Handler **/
protected function onSemesterChange():void
{
	infoObject["program"] = programCombo.selectedItem.id;
	infoObject["branch"] = branchCombo.selectedItem.id;
	infoObject["specialization"] = specializationCombo.selectedItem.id;
	infoObject["semester"]=semesterCombo.selectedItem.id;
	infoObject["time"]=new Date();
	
	getCourseList.send(infoObject);
	getMarksApprovalDetails();
	refresh();
}

/** Method to get details **/
public function getMarksApprovalDetails():void
{
	getApprovalDetails.send(infoObject);
	Mask.show(commonFunction.getMessages('pleaseWait'));
}

/** get details success handler **/
public function getApprovalDetailsSuccess(event:ResultEvent):void
{
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    marksApprovalDetails=event.result as XML;
	var approvalData:ArrayCollection=new ArrayCollection();
	
	for each(var obj:Object in marksApprovalDetails.approvalInfo){
		approvalData.addItem({select:false,programCourseKey:obj.programCourseKey,courseCode:obj.courseCode,courseName:obj.courseName,
							employeeCode:obj.employeeCode,employeeName:obj.employeeName,approvalOrder:obj.approvalOrder});
	}
	marksApprovalGrid.dataProvider=approvalData;
	editButton.enabled=false;
	deleteButton.enabled=false;
	
	marksApprovalCanvas.visible=(approvalData.length>0);
}

/** Method to refresh fields  **/
public function refresh():void
{
	courseCodeCombo.selectedIndex=-1
	courseCodeCombo.errorString="";
	employeeCombo.selectedIndex=-1;
	employeeCombo.errorString="";
	courseNameField.text="";
	employeeNameField.text="";
}

/** get course success **/
public function getCourseSuccess(event:ResultEvent):void
{
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    courseList=event.result as XML;
	
	courseCodeCombo.dataProvider=courseList.courseInfo;
	courseCodeCombo.labelField="courseCode";
	
	courseCodeCombo.enabled=true;
}

/** Course Code change Handler **/
protected function onCourseChange():void
{
	courseNameField.text=courseCodeCombo.selectedItem.courseName;
	employeeCombo.enabled=true;
}

/** get employee success handler **/
public function getEmployeeSuccess(event:ResultEvent):void
{
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    employeeList=event.result as XML;
	
	employeeCombo.dataProvider=employeeList.employeeInfo.employeeCode;
}

/** on change of employeee **/
protected function onEmployeeChange():void
{
	try{
		employeeNameField.text=employeeList.employeeInfo.(employeeCode==employeeCombo.selectedItem).employeeName;
		
	}catch(error:Error)
	{
		employeeNameField.text="";
	}
	approvalOrderField.setFocus();
}

/** on click of submit button **/
public function submitForm():void
{
	if(validateForm())
	{
		infoObject["courseCode"]=courseCodeCombo.selectedLabel;
		infoObject["employeeCode"]=employeeCombo.selectedLabel;
		infoObject["approvalOrder"]=approvalOrderField.value;
		infoObject["programCourseKey"]=courseList.courseInfo.(courseCode==courseCodeCombo.selectedLabel).programCourseKey;
		infoObject["time"]=new Date();
		
		getDuplicateRecordCount.send(infoObject);
	Mask.show(commonFunction.getMessages('pleaseWait'));
	}
	else
	{
		Alert.show(commonFunction.getMessages('pleasecheckEntriesinRed'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	}
}

/** get duplicate success handler **/
public function getDuplicateRecordCountSuccess(event:ResultEvent):void
{
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    if(int(event.result.count)==0)
	{
		Alert.show(commonFunction.getMessages('conformForContinue'),commonFunction.getMessages('confirm'),(Alert.YES|Alert.NO),null,insertOrNot,questionIcon);
	}
	else
	{
		Alert.show(commonFunction.getMessages('duplicateEntry'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	}
}

/** on click of insert confirmation **/
public function insertOrNot(event:CloseEvent):void{
	if(event.detail==Alert.YES)
	{
		infoObject["time"]=new Date();
		setApprovalDetails.send(infoObject);
	}
}

/** get approval details success **/
public function setApprovalDetailsSuccess(event:ResultEvent):void
{
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    if(event.result.info+""=="true")
	{
		Alert.show(commonFunction.getMessages('successOnInsert'),commonFunction.getMessages('success'),0,null,onOK,successIcon);
	}
}

/** on click of ok **/
public function onOK(event:CloseEvent):void
{
	infoObject["time"]=new Date();
	getMarksApprovalDetails();
	refresh();
}

/** validate form data **/
public function validateForm():Boolean
{
	var b:Boolean=true;
	programValidator.source=programCombo;
	branchValidator.source=branchCombo;
	specializationValidator.source=specializationCombo;
	semesterValidator.source=semesterCombo;
	courseCodeValidator.source=courseCodeCombo;
	
	if(Validator.validateAll([programValidator,branchValidator,specializationValidator,semesterValidator,courseCodeValidator]).length>0)
	{
		b=false;
	}
	
	if(employeeNameField.text=="")
	{
		b=false;
		employeeCombo.errorString=commonFunction.getMessages('invalidEntry');
	}
	else
	{
		employeeCombo.errorString="";
	}
	return b;
}

/** on click of edit button **/
public function onEditClick():void
{
	var selectedData:ArrayCollection=commonFunction.getSelectedRowData(marksApprovalGrid);
	
	var editWindow:EditCourseMarksApproval=EditCourseMarksApproval(PopUpManager.createPopUp(this,EditCourseMarksApproval,true));
	
	editWindow.refFunction=getMarksApprovalDetails;
	editWindow.employeeXml=employeeList;
	editWindow.employeeId=selectedData.getItemAt(0).employeeCode;
	editWindow.employeeNameField.text=selectedData.getItemAt(0).employeeName;
	editWindow.programCourseKey=selectedData.getItemAt(0).programCourseKey;
	editWindow.programField.text=programCombo.selectedLabel;
	editWindow.branchField.text=branchCombo.selectedLabel;
	editWindow.specializationField.text=specializationCombo.selectedLabel;
	editWindow.semesterField.text=semesterCombo.selectedLabel;
	editWindow.courseCodeField.text=selectedData.getItemAt(0).courseCode;
	editWindow.courseNameField.text=selectedData.getItemAt(0).courseName;
	editWindow.approvalOrderField.text=selectedData.getItemAt(0).approvalOrder;
	
	PopUpManager.centerPopUp(editWindow);
}

/** on click of delete button **/
public function onDeleteClick():void
{
	Alert.show(commonFunction.getMessages('conformForContinue'),commonFunction.getMessages('confirm'),(Alert.YES|Alert.NO),null,deleteOrNot,questionIcon);
}

/** on click of delete confirmation **/
public function deleteOrNot(event:CloseEvent):void
{
	if(event.detail==Alert.YES)
	{
		var selectedData:ArrayCollection=commonFunction.getSelectedRowData(marksApprovalGrid);
		infoObject['programCourseKey']="";
		infoObject['courseCode']="";
		infoObject['approvalOrder']="";
		
		for each(var obj:Object in selectedData)
		{
			infoObject['programCourseKey']+=obj.programCourseKey+"|";
			infoObject['courseCode']+=obj.courseCode+"|";
			infoObject['approvalOrder']+=obj.approvalOrder+"|";
		}
		
		infoObject["time"]=new Date();
		
		deleteApprovalDetails.send(infoObject);
	Mask.show(commonFunction.getMessages('pleaseWait'));
	}
}
	
/** delete approval details succes handler **/
public function deleteApprovalDetailsSuccess(event:ResultEvent):void
{
	Mask.close();
	try
	{
		if(event.result.sessionConfirm == true)
		{
	 		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	    	this.parentDocument.vStack.selectedIndex=0;
	    	this.parentDocument.loaderCanvas.removeAllChildren();
		}
	}
 	catch(e:Error){}
    
    if(int(event.result.count)>0)
	{
		Alert.show(event.result.count+" "+commonFunction.getMessages('recordsDeletedSuccessfully'),commonFunction.getMessages('success'),0,null,onOK,successIcon);
	}
}